package com.qiuzhiyi.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qiuzhiyi.R;
import com.qiuzhiyi.activity.MsgListActivity;
import com.qiuzhiyi.adapter.MsgTypeAdapter;
import com.qiuzhiyi.base.QZYBaseFragment;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.MsgTypeItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;

import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/13
 * Description: "消息"
 */

public class MsgFragment extends QZYBaseFragment {


    @BindView(R.id.recycler_msg)
    RecyclerView mRecycler;


    private MsgTypeAdapter mAdapter;
    private List<MsgTypeItem> mList;
    public static MsgFragment newInstance() {

        Bundle bundle = new Bundle();
        MsgFragment fragment = new MsgFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mAdapter = new MsgTypeAdapter(R.layout.item_msg_type);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));

        mAdapter.bindToRecyclerView(mRecycler);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String id= String.valueOf(mList.get(position).getId());
                Bundle bundle=new Bundle();
                bundle.putString("id",id);
                bundle.putString("title",mList.get(position).getRemark());
                startWithArg(MsgListActivity.class,bundle);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_msg;
    }

    @Override
    protected void onFirstVisible() {
        super.onFirstVisible();
        showMsgType();
    }

    private void showMsgType() {
        ObservableHelper.createObservable(RetrofitHelper.createService().getMsgType())
                .subscribe(new SelectorObserver<BaseItem<List<MsgTypeItem>>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseItem<List<MsgTypeItem>> item) {
                        mList=item.getObjValue();
                        mAdapter.setNewData(mList);
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        showLoadingDialog();
                    }
                });
    }
}
